﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOADAL
{
    public class NetDisktbDAO
    {
        /// <summary>
        /// 保存网络硬盘新创建的目录信息
        /// </summary>
        /// <param name="ndName"></param>
        /// <param name="ndPath"></param>
        /// <param name="ndob"></param>
        /// <returns></returns>
        public int saveNetDiskCatalogInfo(string ndName, string ndPath, string ndob)
        {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "insert into NetDisk values(@ndName, @ndPath, @ndob, default,@nbTime)";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@ndName", ndName));
                sqlCmd.Parameters.Add(new SqlParameter("@ndPath", ndPath));
                sqlCmd.Parameters.Add(new SqlParameter("@ndob", ndob));
                sqlCmd.Parameters.Add(new SqlParameter("@nbTime",DateTime.Now));
                count = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }

        /// <summary>
        /// 查找所有网络硬盘目录
        /// </summary>
        /// <returns></returns>
        public List<NetDisktb> findAll()
        {
            List<NetDisktb> list = null;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            SqlDataReader sqlDr = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "select * from NetDisk order by NetDiskCatalogId desc";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlDr = sqlCmd.ExecuteReader();
                if (sqlDr.HasRows)
                {
                    SectionDAO sectionDAO = new SectionDAO();
                    list = new List<NetDisktb>();
                    while (sqlDr.Read())
                    {
                        NetDisktb netDisk = new NetDisktb();
                        netDisk.NetDiskCatalogId = (int)sqlDr["NetDiskCatalogId"];
                        netDisk.NetDiskCatalogName = sqlDr["NetDiskCatalogName"].ToString();
                        netDisk.NetDiskCatalogPath = sqlDr["NetDiskCatalogPath"].ToString();
                        netDisk.Pcps = sqlDr["NetDiskCatalogOpenBound"].ToString();
                        netDisk.NetDiskCatalogOpUsers = (string)sqlDr["NetDiskCatalogOpUser"];
                        string ndcob = sqlDr["NetDiskCatalogOpenBound"].ToString();
                        if (ndcob != null && !"".Equals(ndcob.Trim()))
                        {
                            if ("0".Equals(ndcob.Trim()))
                            {
                                netDisk.PcpNames = "全体人员";
                            }
                            else
                            {
                                string[] ndcobs = ndcob.Split(',');
                                for (int i = 0; i < ndcobs.Length; i++)
                                {
                                    if (!"".Equals(ndcobs[i].Trim()))
                                    {
                                        if ("0".Equals(ndcobs[i].Trim()))
                                        {
                                            netDisk.PcpNames = "全体人员";
                                            break;
                                        }
                                        else
                                        {
                                            if (i == ndcobs.Length - 1)
                                            {
                                                netDisk.PcpNames += sectionDAO.findBySectionId(int.Parse(ndcobs[i])).SectionName;
                                            }
                                            else
                                            {
                                                netDisk.PcpNames += (sectionDAO.findBySectionId(int.Parse(ndcobs[i])).SectionName + "<br />");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            netDisk.PcpNames = "暂不开放";
                        }
                        list.Add(netDisk);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return list;
        }

        /// <summary>
        /// 根据共享目录编号查找共享目录信息
        /// </summary>
        /// <param name="ndid"></param>
        /// <returns></returns>
        public NetDisktb findByNdId(int ndid)
        {
            NetDisktb netDisk = null;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            SqlDataReader sqlDr = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "select * from NetDisk where NetDiskCatalogId = @ndid";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@ndid", ndid));
                sqlDr = sqlCmd.ExecuteReader();
                if (sqlDr.HasRows)
                {
                    SectionDAO sectionDAO = new SectionDAO();
                    netDisk = new NetDisktb();
                    sqlDr.Read();
                    netDisk.NetDiskCatalogId = (int)sqlDr["NetDiskCatalogId"];
                    netDisk.NetDiskCatalogName = sqlDr["NetDiskCatalogName"].ToString();
                    netDisk.NetDiskCatalogPath = sqlDr["NetDiskCatalogPath"].ToString();
                    netDisk.Pcps = sqlDr["NetDiskCatalogOpenBound"].ToString();
                    netDisk.NetDiskCatalogOpUsers = (string)sqlDr["NetDiskCatalogOpUser"];
                    string ndcob = sqlDr["NetDiskCatalogOpenBound"].ToString();
                    if (ndcob != null && !"".Equals(ndcob.Trim()))
                    {
                        if ("0".Equals(ndcob.Trim()))
                        {
                            netDisk.PcpNames = "全体人员";
                        }
                        else
                        {
                            string[] ndcobs = ndcob.Split(',');
                            for (int i = 0; i < ndcobs.Length; i++)
                            {
                                if (!"".Equals(ndcobs[i].Trim()))
                                {
                                    if ("0".Equals(ndcobs[i].Trim()))
                                    {
                                        netDisk.PcpNames = "全体人员";
                                        break;
                                    }
                                    else
                                    {
                                        if (i == ndcobs.Length - 1)
                                        {
                                            netDisk.PcpNames += sectionDAO.findBySectionId(int.Parse(ndcobs[i])).SectionName;
                                        }
                                        else
                                        {
                                            netDisk.PcpNames += (sectionDAO.findBySectionId(int.Parse(ndcobs[i])).SectionName + "<br />");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        netDisk.PcpNames = "暂不开放";
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return netDisk;
        }

        /// <summary>
        /// 更新共享目录信息
        /// </summary>
        /// <param name="caName"></param>
        /// <param name="pcps"></param>
        /// <param name="caid"></param>
        /// <returns></returns>
        public int UpdateCatalogInfo(string caName, string pcps, int caid)
        {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "update NetDisk set NetDiskCatalogName = @caName, NetDiskCatalogOpenBound = @pcps where NetDiskCatalogId = @caid";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@caName", caName));
                sqlCmd.Parameters.Add(new SqlParameter("@pcps", pcps));
                sqlCmd.Parameters.Add(new SqlParameter("@caid", caid));
                count = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }

        /// <summary>
        /// 为指定共享目录增加可操作人员信息
        /// </summary>
        /// <param name="opUsers"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public int AddCatalogOpUsers(string opUsers, int cid)
        {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "update NetDisk set NetDiskCatalogOpUser = @opUsers where NetDiskCatalogId = @cid";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@opUsers", opUsers));
                sqlCmd.Parameters.Add(new SqlParameter("@cid", cid));
                count = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }

        /// <summary>
        /// 根据目录编号删除目录信息
        /// </summary>
        /// <param name="ndid"></param>
        /// <returns></returns>
        public int DelCatalogInfoById(int ndid)
        {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "delete from NetDisk where NetDiskCatalogId = @ndid";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@ndid", ndid));
                count = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConn != null & sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }
    }
}
